<!DOCTYPE html>
<html lang="zh-CN" xmlns:layOut="http://www.w3.org/1999/xhtml" layOut:decorator="layOut/layOut">
<head>
    <title>角色管理</title>
</head>
<body class="pear-container">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form">
                <div class="layui-form-item">
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label" for="name">角色名</label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" id="name" placeholder="请输入" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="theSearchFilter">
                            <i class="layui-icon layui-icon-search"></i>查询
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="theTable" lay-filter="tableFilter"></table>
        </div>
    </div>

    <script type="text/html" id="topToolbar">
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="addEvent">
            <i class="layui-icon layui-icon-add-1"></i>新增
        </button>
        <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemoveEvent">
            <i class="layui-icon layui-icon-delete"></i>删除
        </button>
    </script>
    <script type="text/html" id="innerToolbar">
        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="editEvent">
            <i class="layui-icon layui-icon-edit"></i>
        </button>
        <button class="pear-btn pear-btn-warming pear-btn-sm" lay-event="configEvent">
            <i class="layui-icon layui-icon-vercode"></i>
        </button>
    </script>

    <script type="text/javascript">
        layui.use(['table', 'form', 'jquery','common'], function() {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;
            let common = layui.common;

            table.render({
                elem: '#theTable',
                url: '/role/list/',
                method: 'post',
                page: true,
                skin: 'line',
                toolbar: '#topToolbar',
                defaultToolbar: [{
                    title: '刷新',
                    layEvent: 'refreshEvent',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports'],
                cols: [ [
                    {type: 'checkbox'},
                    {field: 'serialNumber', align: 'center', title: '序号', type: 'numbers'},
                    {field: 'name', align: 'center', title: '角色名',
                        templet: function (data) {
                            var name = data.name;
                            return "<a style='color:#01AAED;cursor: pointer'>" + name + "</a>";
                        }
                    },
                    {field: 'code', align: 'center', title: '角色码'},
                    {field: 'status', align: 'center', title: '状态'},
                    {field: 'introduction', align: 'center', title: '介绍'},
                    {field: 'updated', align: 'center', title: '更新时间'},
                    {title: '操作', toolbar: '#innerToolbar', align: 'center', width: 130,}
                ] ]
            });
            //表格中工具栏
            table.on('tool(tableFilter)', function(obj) {
                if (obj.event === 'editEvent') {
                    layer.open({
                        type: 2,
                        title: '修改',
                        shade: 0.1,
                        area: ['500px', '400px'],
                        content: '/role/edit/'+obj.data.id
                    });
                } else if (obj.event === 'configEvent') {
                    layer.open({
                        type: 2,
                        title: '配置',
                        shade: 0.1,
                        area: ['500px', '400px'],
                        content: '/role/config/'+obj.data.id
                    });
                }
            });
            //表格顶部工具栏
            table.on('toolbar(tableFilter)', function(obj) {
                if (obj.event === 'addEvent') {
                    layer.open({
                        type: 2,
                        title: '新增',
                        shade: 0.1,
                        area: [common.isModile()?'100%':'500px', common.isModile()?'100%':'400px'],
                        content: '/role/add'
                    });
                } else if (obj.event === 'refreshEvent') {
                    table.reload('theTable');
                } else if (obj.event === 'batchRemoveEvent') {
                    window.batchRemove(obj);
                }
            });
            //搜索
            form.on('submit(theSearchFilter)', function(data) {
                table.reload('theTable', {
                    where: data.field
                })
                return false;
            });
            //批量删除
            window.batchRemove = function(obj) {
                var checkIds = common.checkField(obj,'id');
                if (checkIds === "") {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }
                layer.confirm('确定要删除', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: "del/" + checkIds,
                        dataType: 'json',
                        type: 'post',
                        success: function(result) {
                            layer.close(loading);
                            if (result.code === 1) {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function() {
                                    table.reload('theTable');
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }
        })
    </script>
</body>
</html>